import 'package:flutter_admin/pages/common/cry/cry.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/purchase_evaluation_detail_odoo_api.dart';
import 'package:flutter_admin/constants/enum.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/purchase_evaluation_detail.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';

class PurchaseEvaluationDetailList extends StatefulWidget {

  String evaluationId;
  PurchaseEvaluationDetailList({Key? key,required this.evaluationId}) : super(key: key);

  @override
  _PurchaseEvaluationDetailListState createState() => _PurchaseEvaluationDetailListState();
}

class _PurchaseEvaluationDetailListState extends State<PurchaseEvaluationDetailList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page = PageModel(orders: [OrderItemModel(column: 'create_date',asc: false)]);
  PurchaseEvaluationDetail purchaseEvaluationDetail = PurchaseEvaluationDetail();
  @override
  void initState() {
    super.initState();
    purchaseEvaluationDetail.evaluationId = widget.evaluationId;
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {

    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: [
        DataColumn(
          label: Text('考评模板'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('考评项目'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('考核内容'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('评估标准'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('权重分数'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('实际详情'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('实际得分'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('考评意见'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
      ],
      getCells: (m) {
        PurchaseEvaluationDetail purchaseEvaluationDetail = PurchaseEvaluationDetail.fromMap(m);
        return [
          DataCell(Text(purchaseEvaluationDetail.templateName ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.projectName ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.assessmentContent ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.evaluationCriteria ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.weightScore ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.actualDetails ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.actualScore ?? '--')),
          DataCell(Text(purchaseEvaluationDetail.evaluationOpinions ?? '--')),
        ];
      },
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text('明细数据'),
      ),
      key: scaffoldStateKey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return result;
  }



  _query() {
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi = await PurchaseEvaluationDetailOdooApi.page(
        data:{"page": page,"domainList":purchaseEvaluationDetail.toDomainList()});
    if(responseBodyApi.success!){
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
    }
  }

}